﻿using SlaughterItem.Common.Domains.UPCTable;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;

namespace WinFormsApp1.TongShiWei.UPC
{
    public partial class AddUPCData : UIForm
    {
        public AddUPCData()
        {
            InitializeComponent();
            //显示商品分类数据
            ShowUPCTypeData();
            //显示商品等级
            ShowUPCGradeData();
        }
        #region 获取商品分类表数据
        public async void ShowUPCTypeData()
        {
            List<UPCTypeData> uPCTypeDatas = new List<UPCTypeData>()
            {
                new UPCTypeData{ CommodityId=0, CommodityName="请选择商品分类",IsCommodity=false,Pid=0}
            };
            //http://localhost:5159/api/TSW/GetUPCTypeData
            var res = await HttpClientHelper.RequestUrl<UPCTypeData, ApiResult<List<UPCTypeData>>>(URLCommon.Url2, "/TSW/GetUPCTypeData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                uPCTypeDatas.AddRange(res.Data);
                this.CommodityId_.DataSource = uPCTypeDatas;
                this.CommodityId_.ValueMember = "CommodityId";
                this.CommodityId_.DisplayMember = "CommodityName";
            }
        }
        public async void ShowUPCGradeData()
        {
            List<UPCGradeData> list = new List<UPCGradeData>()
            {
                new UPCGradeData(0,"请选择等级"),
                new UPCGradeData(1,"一级"),
                new UPCGradeData(2,"二级"),
                new UPCGradeData(3,"三级"),
                new UPCGradeData(4,"等级"),
            };
            this.GoodsGradeId_.DataSource = list;
            this.GoodsGradeId_.DisplayMember = "GoodsGradName_";
            this.GoodsGradeId_.ValueMember = "GoodsGradeId_";
        }
        public record UPCGradeData(int GoodsGradeId_, string GoodsGradName_);
        public class UPCTypeData
        {
            /// <summary>
            /// 商品分类ID
            /// </summary>
            public int CommodityId { get; set; }
            /// <summary>
            /// 商品分类名称
            /// </summary>
            public string CommodityName { get; set; } = string.Empty;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsCommodity { get; set; } = false;
            /// <summary>
            /// 区别字段{1:活畜 2:(白条||销售) 3:分割后 4:膘油}
            /// </summary>
            public int Pid { get; set; } = 0;
        }
        #endregion
        #region
        public async void AddUPCData_()
        {
            UPCData uPCData = new UPCData()
            {
                GoodsMinuteId = 0,
                MemoryNum = this.MemoryNum_.Text,
                CommodityId = Convert.ToInt32(this.CommodityId_.SelectedValue),
                OrderNum = 0,
                GoodsGradeId = Convert.ToInt32(this.GoodsGradeId_.SelectedValue),
                UPCDataName = this.UPCDataName_.Text,
                IsGoodsMinute = false
            };
            //http://localhost:5159/api/TSW/AddUPCData
            var res = await HttpClientHelper.RequestUrl<UPCData, ApiResult<UPCData>>(URLCommon.Url1, "/TSW/AddUPCData", HttpClientHelper.MethodType.POST, uPCData);
            if (res.Code > 0)
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }
        //添加商品代号数据
        public class UPCData
        {
            /// <summary>
            /// 商品代号ID
            /// </summary>
            public int GoodsMinuteId { get; set; }
            /// <summary>
            /// 助记码
            /// </summary>
            public string MemoryNum { get; set; } = string.Empty;
            /// <summary>
            /// 商品分类Id
            /// </summary>
            public int CommodityId { get; set; }
            /// <summary>
            /// 顺序号 
            /// </summary>
            public int OrderNum { get; set; }
            /// <summary>
            /// 等级ID
            /// </summary>
            public int GoodsGradeId { get; set; }
            /// <summary>
            /// 商品代号名称
            /// </summary>
            public string UPCDataName { get; set; } = string.Empty;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsGoodsMinute { get; set; } = false;
        }
        #endregion
        //关闭当前页面
        private void uiButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //添加
        private void uiButton1_Click(object sender, EventArgs e)
        {
            AddUPCData_();
        }
    }
}
